// 数据库名称
const databaseName = 'tmatrixcodegeneratordemoforweb'
// 响应
function response(code, msg, data) { return { code, msg, data } }

// 成功
function success(data = null) { return response(200, '操作成功', data) }

// 失败
function failure(data = null) { return response(400, '操作失败', data) }

// 时间戳
function timestamp() { return Date.now() }

// 开启数据库 没数据库就创建
function open() {
  return new Promise((resolve, reject) => {
    const database = indexedDB.open(databaseName, timestamp())
    database.onsuccess = (e) => resolve(e.target.result)
    database.onerror = (e) => reject(e)
  })
}

// 更新数据库
function update() {
  return new Promise((resolve, reject) => {
    const database = indexedDB.open(databaseName, timestamp())
    database.onupgradeneeded = (e) => resolve(e.target.result)
    database.onerror = (e) => reject(e)
  })
}

/** 获取仓库列表 */
export function deleteDatabase() {
  return new Promise((resolve) => {
    const result = window.indexedDB.deleteDatabase(databaseName);
    result.onsuccess = (e) => resolve(success('删除成功'))
    result.onerror = (e) => resolve(failure(e))
  })
}

/** 获取仓库列表 */
export function getStoreList() {
  return new Promise(async resolve => {
    const database = await open(databaseName)
    const result = Object.values(database.objectStoreNames)
    resolve(success(result))
    database.close()
  })
}

/** 判断仓库是否存在 */
export function getStoreExist(storeName) {
  return new Promise(async resolve => {
    const database = await open(databaseName)
    // 判断该名称是否存在
    if (database.objectStoreNames.contains(storeName)) {
      resolve(success(true))
    } else {
      resolve(success(false))
    }
    database.close()
  })
}

/** 创建仓库 */
export function createStore(storeName) {
  return new Promise(async resolve => {
    const database = await update(databaseName)
    // 判断该名称是否存在
    if (!database.objectStoreNames.contains(storeName)) {
      database.createObjectStore(storeName)
      resolve(success('创建仓库成功'))
    } else {
      resolve(failure('该名称项目已存在'))
    }
    database.close()
  })
}

/** 删除仓库 */
export function deleteStore(storeName) {
  return new Promise(async resolve => {
    const database = await update(databaseName)
    // 判断该名称是否存在
    if (database.objectStoreNames.contains(storeName)) {
      database.deleteObjectStore(storeName)
      resolve(success('删除仓库成功'))
    } else {
      resolve(failure('仓库不存在'))
    }
    database.close()
  })
}

/** 向仓库添加数据 */
export function addStoreValue(storeName, key, value) {
  return new Promise(async resolve => {
    const database = await open(databaseName)
    const transaction = database.transaction([storeName], 'readwrite')
    const objectStore = transaction.objectStore(storeName)
    const operate = objectStore.add(value, key)
    const result = await new Promise((resolve) => {
      operate.onsuccess = (e) => resolve(success(e.target.result))
      operate.onerror = (e) => resolve(failure(e.target.error))
    })
    resolve(result)
    database.close()
  })
}

/** 修改仓库某条数据 */
export function putStoreValue(storeName, key, value) {
  return new Promise(async resolve => {
    const database = await open(databaseName)
    const transaction = database.transaction([storeName], 'readwrite')
    const objectStore = transaction.objectStore(storeName)
    const operate = objectStore.put(value, key)
    const result = await new Promise((resolve) => {
      operate.onsuccess = (e) => resolve(success(e.target.result))
      operate.onerror = (e) => resolve(failure(e.target.error))
    })
    resolve(result)
    database.close()
  })
}

/** 获取仓库中数据 */
export function getStoreValue(storeName, key) {
  return new Promise(async resolve => {
    const database = await open(databaseName)
    const transaction = database.transaction([storeName], 'readonly')
    const objectStore = transaction.objectStore(storeName)
    const operate = objectStore.get(key)
    const result = await new Promise((resolve) => {
      operate.onsuccess = (e) => resolve(success(e.target.result))
      operate.onerror = (e) => resolve(failure(e.target.error))
    })
    resolve(result)
    database.close()
  })
}

/** 获取仓库中所有key */
export function getStoreAllKey(storeName) {
  return new Promise(async resolve => {
    const database = await open(databaseName)
    const transaction = database.transaction([storeName], 'readonly')
    const objectStore = transaction.objectStore(storeName)
    const operate = objectStore.getAllKeys()
    const result = await new Promise((resolve) => {
      operate.onsuccess = (e) => resolve(success(e.target.result))
      operate.onerror = (e) => resolve(failure(e.target.error))
    })
    resolve(result)
    database.close()
  })
}

/** 获取仓库中所有value */
export function getStoreAllValue(storeName) {
  return new Promise(async resolve => {
    const database = await open(databaseName)
    const transaction = database.transaction([storeName], 'readonly')
    const objectStore = transaction.objectStore(storeName)
    const operate = objectStore.getAll()
    const result = await new Promise((resolve) => {
      operate.onsuccess = (e) => resolve(success(e.target.result))
      operate.onerror = (e) => resolve(failure(e.target.error))
    })
    resolve(result)
    database.close()
  })
}